package main

import (
	"bytes"
	"fmt"
	log "github.com/sirupsen/logrus"
	"os"
)

var logger = log.New()

func init() {
	logger.SetOutput(os.Stdout)

	log.SetFormatter(&log.JSONFormatter{})
	log.SetOutput(os.Stdout)
	log.SetLevel(log.WarnLevel)
}

type DefaultFieldHook struct {
}

func (hook *DefaultFieldHook) Fire(entry *log.Entry) error {
	entry.Data["appName"] = "MyAppName"
	return nil
}

func (hook *DefaultFieldHook) Levels() []log.Level {
	return log.AllLevels
}

func doLog() {
	log.AddHook(&DefaultFieldHook{})
	log.WithFields(log.Fields{"animal": "walrus", "size": 10}).Warn("A walrus appears")

}

func buf1() {
	var buf bytes.Buffer
	buf.WriteString("hello,world")
	buf.WriteString(" ")
	buf.WriteString("end")
	fmt.Println(buf.String())
}

func buf2() {
	buffer := bytes.NewBufferString("hello,world")
	fmt.Println(buffer.String())
}

func buf3() {
	buf := bytes.NewBuffer(nil)
	n, err := buf.Write([]byte("hello,world"))
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("write %d bytes\n", n)
	fmt.Println(buf.String())
}

func main() {
	buf4()
}

func buf4() {
	buf := bytes.NewBufferString("hello")
	data := make([]byte, 5)
	n, err := buf.Read(data)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("read %d bytes\n", n)
	fmt.Println(string(data))
}
